﻿
using System.Web.Security; 
using Stimulus360.Repositories.Interfaces; 

namespace Stimulus360.Repositories
{

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Aspnet", Justification ="Aspnet is a valid term")]
    public class AspnetMembershipRepository : IMembershipRepository
    {
        /// <summary>
        ///     Instance of Membership provider to be used
        /// </summary>
        private readonly MembershipProvider _provider;

        /// <summary>
        ///     Default constructor
        /// </summary>
        public AspnetMembershipRepository()
            : this(null)
        {
        }
        
        /// <summary>
        ///     Constructor to inject the membership provider to be used
        /// </summary>
        /// <param name="provider">
        ///     Membership provider to be used
        /// </param>
        public AspnetMembershipRepository(MembershipProvider provider)
        {
            _provider = provider ?? Membership.Provider;
        }
       
        #region IUserAccountRepository Members

        /// <summary>
        ///     The miminum allowed password length
        /// </summary>
        public int MinPasswordLength
        {
            get
            {
                return _provider.MinRequiredPasswordLength;
            }
        }

        /// <summary>
        ///     Method to validate the user 
        /// </summary>
        /// <param name="userName">
        ///     Username provided for validation
        /// </param>
        /// <param name="password">
        ///     Password provided for validation
        /// </param>
        /// <returns>
        ///     Bool indicating if user is valid or not.
        /// </returns>
        public bool ValidateUser(string userName, string password)
        {
            return _provider.ValidateUser(userName, password);
        }

        /// <summary>
        ///     Method to create a new user in the system
        /// </summary>
        /// <param name="userName">
        ///     The username provided by the user
        /// </param>
        /// <param name="password">
        ///     The Password provided by the user
        /// </param>
        /// <param name="email">
        ///     The email provided by the user
        /// </param>
        /// <returns>
        ///     Membership status.
        /// </returns>
        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status;
            _provider.CreateUser(userName, password, email, null, null, true, null, out status);
            return status;
        }

        /// <summary>
        ///     Method to allow the user change his password
        /// </summary>
        /// <param name="userName">
        ///     The Username
        /// </param>
        /// <param name="oldPassword">
        ///     The existing password
        /// </param>
        /// <param name="newPassword">
        ///     The new password
        /// </param>
        /// <returns>
        ///     Bool indicating if password was successfully changed
        /// </returns>
        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */);
            return currentUser.ChangePassword(oldPassword, newPassword);
        }

        /// <summary>
        ///     Method to check if a username already exists
        /// </summary>
        /// <param name="userName">
        ///     The username to check
        /// </param>
        /// <returns>
        ///     Bool indicating if the username is already in use.
        /// </returns>
        public bool UserExists(string userName)
        {
            MembershipUser newUser = _provider.GetUser(userName, true /* userIsOnline */);
            if (newUser == null)
            {
                return false;
            }
            return true;
        }

        #endregion
    }
}
